import javaobj
import redis


class RedisUtil:
    # decode_responses 设置True以字符串的形式得到数据 设置False 二进制格式数据
    def __init__(self,host,password,port=6379,decode_responses=False):
        self.pool = redis.ConnectionPool(host=host,
                             port=port,
                             password=password,
                             decode_responses=decode_responses,
                             encoding_errors='ignore')
        self.r = redis.Redis(connection_pool=self.pool)

    def get(self,key):
        type=self.r.type(key).decode('utf8')
        if type=='string':
            return self.r.get(key)
        elif type=='hash':
            return self.r.hgetall(key)
        elif type=='list':
            return self.r.lrange(key,0,-1)
        elif type=='zset':
            return self.r.zrange(key,0,-1)
        elif type=='set':
            return self.r.smembers(key)
        else:
            raise Exception(f'不支持数据类型{type}或者{key}不存在')

if __name__ == '__main__':
    redis_util = RedisUtil(host='82.156.74.26',password='mtx')
    res = redis_util.get('{BUY_NOW_ORIGIN_DATA_PREFIX}_24216')
    print(res)
    #  无法直接解析 res
    # 通过 javaobj-py3 将java对象转换Python对象
    # pip install javaobj-py3 -i https://pypi.douban.com/
    res_list = javaobj.loads(res)
    print(res_list)
    obj = res_list[0]
    # 通过dir方法查看obj对象属性
    print(dir(obj))
    # 获取skuid 和 num
    print(obj.__getattribute__('num'))
    print(obj.__getattribute__('skuId'))